package com.example.walkera.rtsp_android_client.activity;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.walkera.rtsp_android_client.R;
import com.example.walkera.rtsp_android_client.function.WifiCheck;

import java.util.IllegalFormatCodePointException;
import java.util.List;

/**
 * Created by kaifa@lihaiping1603@aliyun.com on 2016/12/29.
 */
public class LHPWifiListAdapter extends BaseAdapter {
    private final static String TAG="LHPWifiListAdapter";
    LayoutInflater inflater;
    List<ScanResult> list;
    private Context context;

    private boolean bSelected;
    private int currentSelected;
    private String mStrConnectSSID;

    //屏幕的高度
    private int mScreenHeight=0;
    private int mCountOneScreen;//一屏能显示的item个数
    private int mChildHeight=0;//子控件高度

    public LHPWifiListAdapter(Context context, List<ScanResult> list,String strConnectSSID) {
        // TODO Auto-generated constructor stub
        this.inflater = LayoutInflater.from(context);
        this.list = list;
        this.context = context;
        this.bSelected=false;
        this.currentSelected=0;
        mStrConnectSSID=strConnectSSID;

        // 获得屏幕宽度
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        mScreenHeight = outMetrics.heightPixels;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position).SSID;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        //////////////////////
        ViewHolder holder=null;
        View view=convertView;
        if (view==null){
            view = inflater.inflate(R.layout.wifi_scan_list, parent,false);
            holder=new ViewHolder();
            assert view != null;
//            holder.textViewWifiName =(TextView) view.findViewById(R.id.scan_wifi_ssid);
            holder.textViewWifiName =(CheckedTextView) view.findViewById(R.id.scan_wifi_ssid);
            holder.imageViewWifiLevel = (ImageView) view.findViewById(R.id.wifi_strong_level);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }

        if (mChildHeight==0){
            int w = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);
            view.measure(w, h);
            mChildHeight = view.getMeasuredHeight();
            Log.d(TAG, "getView: childHeight:"+mChildHeight);
            // 计算一屏能加载多少个
            mCountOneScreen = (mScreenHeight / mChildHeight == 0)?mScreenHeight / mChildHeight+1:mScreenHeight / mChildHeight+2;
        }
        ScanResult scanResult = list.get(position);
        holder.textViewWifiName.setText(scanResult.SSID);

        //计算信号等级
        int level = WifiCheck.getWifiStrongLevel(scanResult,5);
        // 判断信号强度，显示对应的指示图标
        if (level == 4) {
            holder.imageViewWifiLevel.setImageDrawable(context.getResources().getDrawable(R.mipmap.wifi_4));
        } else if (level == 3) {
            holder.imageViewWifiLevel.setImageDrawable(context.getResources().getDrawable(R.mipmap.wifi_3));
        } else if (level <= 2) {
            holder.imageViewWifiLevel.setImageDrawable(context.getResources().getDrawable(R.mipmap.wifi_2));
        }

        if(currentSelected==position){
            view.setBackgroundResource(R.color.pink);
        }
        else{
            view.setBackgroundResource(R.color.listitem_transparent);
        }

        //判断是否已经连接
//        if (mStrConnectSSID==scanResult.SSID){//这个结果不准确
        if(scanResult.SSID.compareTo(mStrConnectSSID)==0){
//            Log.d(TAG, "getView: connectSSID:"+mStrConnectSSID+" scan ssid:"+scanResult.SSID+" position:"+position+" list_size:"+list.size());
            holder.textViewWifiName.setChecked(true);
        }
        else {
//            Log.d(TAG, "getView: scan_ssid:"+scanResult.SSID);
            holder.textViewWifiName.setChecked(false);
        }

        view.invalidate();
        return view;
    }

    /**
     * Added by zhangyanhu C01012,2014-4-4
     */
    private WifiManager getSystemService(String wifiService) {
        // TODO Auto-generated method stub
        return null;
    }

    int getCurrentSelectedIndex(){
        return currentSelected;
    }

    public  void nextItemSelected(){
        if(currentSelected>=0 && currentSelected <list.size()-1) {
            currentSelected++;
            setCurrentItemSelected();
        }
    }
    public  boolean preItemSelected(){
        if(currentSelected>0 && currentSelected <=list.size()-1) {
            currentSelected--;
            setCurrentItemSelected();
            return true;
        }else{
            return false;
        }
    }
    public void downItemSelected(){
        final int colNum = 3;
        int ri = currentSelected/colNum;
        int mi = currentSelected%colNum;

        int index = (ri+1)*colNum + mi;
        if(index < list.size()-1){
            currentSelected  = index;
            setCurrentItemSelected();
        }
    }

    public void upItemSelected(){
        final int colNum = 3;
        int ri = currentSelected/colNum;
        int mi = currentSelected%colNum;

        int index = (ri-1)*colNum + mi;
        if(index >=0 ){
            currentSelected  = index;
            setCurrentItemSelected();
        }

    }

    public  void setCurrentItemSelected(){
        bSelected=true;
        //通知数据有改变
        notifyDataSetChanged();
    }

    public void setIsSelected(boolean isSelected) {
        this.bSelected = isSelected;
    }

    public ScanResult getCurrentItemScanResult(){
        return list.get(currentSelected);
    }

    public boolean shouldScroll(){
        if (currentSelected>(mCountOneScreen-4))
            return true;
        return false;
    }

   private final class ViewHolder{
//       public TextView textViewWifiName;
       public CheckedTextView textViewWifiName;
       public ImageView imageViewWifiLevel;
   }
}
